package generic;

import java.util.ArrayList;
import java.util.Comparator;

/**
 * @author DCSGO
 * @version 1.0
 * 定义Employee类
 * 1)该类包含: private成员变量name,sal,birthday,其中 birthday 为 MyDate 类的对象;
 * 2)为每一个属性定义getter, setter 方法;
 * 3)重写toString方法输出name, sal,birthday
 * 4) MyDate类包含:private成员变量month,day,year;并为每一个属性定义getter setter方法;
 * 5)创建该类的3个对象，并把这些对象放入ArrayList集合中(ArrayList需使用泛型来定义)，对集合中的元素进行排序,
 * 并遍历输出
 * 排序方式: 调用ArrayList 的sort方法，传入 Comparator对象[使用泛型]，先按照name排序，如果name相同，
 * 则按生日日期的先后排序。【即:定制排序】
 */

public class GenericExercise02 {
    public static void main(String[] args) {
        Employee emp1 = new Employee("wang", 12300, new MyDate(2000, 1, 23));
        Employee emp2 = new Employee("zhang", 44300, new MyDate(1995, 3, 5));
        Employee emp3 = new Employee("zhang", 44300, new MyDate(1995, 3, 4));
        Employee emp4 = new Employee("liu", 22350, new MyDate(1998, 7, 18));

        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(emp1);
        employees.add(emp2);
        employees.add(emp3);
        employees.add(emp4);

        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee emp1, Employee emp2) {
                int cmpName = emp1.getName().compareTo(emp2.getName());
                /*if (cmpName != 0) {
                    return cmpName;
                } else {
                    int cmpYear = emp1.getBirthday().getYear() - emp2.getBirthday().getYear();
                    if (cmpYear == 0) {
                        int cmpMonth = emp1.getBirthday().getMonth() - emp2.getBirthday().getMonth();
                        if (cmpMonth == 0) {
                            return emp1.getBirthday().getDay() - emp2.getBirthday().getDay();
                        } else {
                            return cmpMonth;
                        }
                    } else {
                        return cmpYear;
                    }
                }
            }*/
                if (cmpName != 0) {//name 不同
                    return cmpName;
                }
                return emp1.getBirthday().compareTo(emp2.getBirthday());

                /*int cmpYear = emp1.getBirthday().getYear() - emp2.getBirthday().getYear();
                if (cmpYear != 0) {//出生年不同
                    return cmpYear;
                }

                int cmpMonth = emp1.getBirthday().getMonth() - emp2.getBirthday().getMonth();
                if (cmpMonth != 0) {//出生月不同
                    return cmpMonth;
                }
                return emp1.getBirthday().getDay() - emp2.getBirthday().getDay();*/
            }
        });

        for (Employee emp : employees) {
            System.out.println(emp);
        }
        //Employee{name='liu', sal=22350, birthday=MyDate:1998-7-18}
        //Employee{name='wang', sal=12300, birthday=MyDate:2000-1-23}
        //Employee{name='zhang', sal=44300, birthday=MyDate:1995-3-4}
        //Employee{name='zhang', sal=44300, birthday=MyDate:1995-3-5}
    }
}

class Employee {
    private String name;
    private int sal;
    private MyDate birthday;

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    public Employee(String name, int sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }
}

class MyDate implements Comparable<MyDate> {
    private int year;
    private int month;
    private int day;

    public int getYear() {
        return year;
    }

    @Override
    public int compareTo(MyDate date) {
        int cmpYear = year - date.getYear();
        if (cmpYear != 0) {//出生年不同
            return cmpYear;
        }

        int cmpMonth = month - date.getMonth();
        if (cmpMonth != 0) {//出生月不同
            return cmpMonth;
        }
        return day - date.getDay();
    }

    @Override
    public String toString() {
        return "MyDate:" +
                year +
                "-" + month +
                "-" + day;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
}
